// pages/administrate/administrate.js
var choose = require("../../utils/util.js");

var sliderWidth = 96; // 需要设置slider的宽度，用于计算中间位置
Page({
    data: {
        tabs: ["商品管理", "商铺管理"],
        activeIndex: "0",
        sliderOffset: 0,
        sliderLeft: 0,
        shop: null,
        goodType: null,
        isBlock: false,
        currentIndex: null,
        fid: "",
        content: "",
        imgUrl: "",
        img: ""
    },
    onLoad: function () {
        var that = this;
        wx.getSystemInfo({
            success: function (res) {
                that.setData({
                    sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2
                });
            }
        });
        wx.request({
            url: 'http://1.aiym.applinzi.com/aiyouwai.php',
            data: {
                type: 'getAllGoodsType'
            },
            method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
            header: { "Content-Type": "application/json" }, // 设置请求的 header
            success: function (res) {
                // success
                console.log(res)
                that.setData({
                    goodType: res.data.goodType,
                    shop: res.data.shop
                })
            }
        })
    },
    tabClick: function (e) {
        this.setData({
            sliderOffset: e.currentTarget.offsetLeft,
            activeIndex: e.currentTarget.id
        });
    },
    callUser: function (event) {
        wx.makePhoneCall({
            phoneNumber: event.currentTarget.dataset.title
        })
    },
    deleteTarget: function (event) {
        console.log(event)
        var that = this
        wx.showModal({
            title: "提示",
            content: "您将删除该条信息，请确认操作",
            success: function () {
                wx.request({
                    url: 'http://1.aiym.applinzi.com/aiyouwai.php',
                    data: {
                        type: "adminReviseMsg",
                        paras: {
                            theWay: "delete_action",
                            target: event.currentTarget.dataset.title,
                            id: event.currentTarget.id
                        }
                    },
                    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
                    header: { "Content-Type": "application/json" }, // 设置请求的 header
                    success: function (res) {
                        // success
                        console.log(res)
                        if (res.data.errcode == 0) {
                            var b = event.currentTarget.dataset.title == 'good'
                            var a = b ? that.data.goodType : that.data.shop;
                            a.splice(event.currentTarget.dataset.index, 1)
                            if (b) {
                                that.setData({
                                    goodType: a
                                })
                            } else {
                                that.setData({
                                    shop: a
                                })
                            }

                        } else {
                            wx.showToast({
                                title: "删除失败"
                            })
                        }
                    }
                })
            }
        })
    },
    popover: function (event) {
        var para = this.data.goodType[event.currentTarget.dataset.index]
        this.setData({
            isBlock: true,
            fid: para ? para.fid : "",
            content: para ? para.content : "",
            imgUrl: para ? para.imgurl : ""
        })
    },
    onblur: function (e) {
        this.setData({
            content: e.detail.value
        })
    },
    removeBox: function () {
        var that = this
        var obj = {
            fid: this.data.fid,
            content: this.data.content,
            imgUrl: this.data.imgUrl
        }
        var paras = that.data.goodType
        wx.request({
            url: 'http://1.aiym.applinzi.com/aiyouwai.php',
            data: {
                type: "reviseFirstPage",
                paras: obj
            },
            method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
            header: { "Content-type": "application/json" }, // 设置请求的 header
            success: function (res) {
                // success
                paras.push(obj)
                that.setData({
                    isBlock: false,
                    goodType: paras,
                    fid: "",
                    content: "",
                    imgUrl: ""
                })
            },
            complete: function (res) {
                obj = {}
            }
        })
    },
    chooseImg: function () {   // 选择图片并返回路径
        var that = this
        choose.chooseImg(that)
    },
    addNew: function (e) {
        this.setData({
            isBlock: true
        })
    },
    closeBox: function () {
        this.setData({
            isBlock: false
        })
    },
    detailMess: function (e) {
        this.redirect(e.currentTarget.dataset.title)
    },
    redirect: function (param) {
        wx.navigateTo({
            url: '../detailMess/detailMess?param=' + param
        })
    },
    deleteShop: function (e) {
        var that = this
        var uid = e.currentTarget.id
        console.log(uid)
        var shop = that.data.shop
        wx.request({
            url: 'http://1.aiym.applinzi.com/aiyouwai.php',
            data: {
                type: "deleteShops",
                paras: {
                    uid: uid
                }
            },
            method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
            // header: {}, // 设置请求的 header
            success: function (res) {
                // success
                console.log(res)
                if (!res.data.errcode) {
                    wx.showModal({
                        title: "警告",
                        content: "已删除商家"
                    })
                    that.setData({
                        isBlock: false
                    })
                }
            }
        })
    }
})

